<template>
	<!-- 设备列表 -->
	<view class="page-content">
		<view class="page-body">
			<view v-if="datalist && datalist.length > 0 ">
				<block v-for="(item,index) in datalist" :key="index">
					<view class="list-li" @click="tohome(item.model_type)">
						<view class="list-li-l">
							<view class="list-li-l-tit">{{i18n.pair.pairName}}：{{item.name}}</view>
							<text class="list-li-l-mon"
								:style="{background:item.model_type==='1'?'#D70000':'#00CD8B'}">{{item.model_type==='1'?i18n.pair.jihuoban:i18n.pair.tongyongban}}</text>
						</view>
						<view class="list-li-r">
							<view class="icons icon-gengduo"></view>
						</view>
					</view>
				</block>
			</view>
			<!--无数据-->
			<empty v-else no-full type="noData"></empty>
		</view>
	</view>
</template>

<script>
	import {
		getgamegroups //获取赛事组别
	} from '@/common/api.js'
	import {
		mapGetters,
		mapActions
	} from "vuex";
	export default {
		components: {},
		data() {
			return {
				tp_id: '',
				datalist: [{
					name: 'asdfas',
					model_type: '1'
				}, {
					name: 'sdsdsd',
					model_type: '2'
				}, {
					name: 'sfwfwf',
					model_type: '2'
				}]
			}
		},
		computed: {
			...mapGetters([
				"orgInfo"
			]),
			i18n() {
				return this.$t('message');
			}
		},
		onShow() {
			// 导航栏多语言
			uni.setNavigationBarTitle({
				title: this.i18n.tabBar.pair
			});
			this.getdata();

		},
		onLoad(option) {
			console.log(222, option)
			this.tp_id = option.id;
		},
		methods: {
			...mapActions("user", ["setModelType", "setPairStatu"]),
			
			initBluetoothModule() {
				//初始蓝牙模块
				uni.openBluetoothAdapter({
					success: res => {
						this.searchBlueList();
					},
					fail: err => {
						console.log(err)
					}
				})
			},
			searchBlueList() {
				//开启蓝牙搜索
				uni.startBluetoothDevicesDiscovery({
					success: res => {
						setTimeout(() => {
							this.getBlueList();
							uni.showToast({
								title: '开启成功',
								icon: 'success',
								duration: 1000
							});
						}, 1000);
					},
				})
			},
			getBlueList() {
				//获取搜索列表
				uni.getBluetoothDevices({
					success: res => {
						let data = res.devices
						let tempList = [];
						data.map(device => {
							if (!!device.localName) {
								tempList.push(device)
							}
						});
						this.deviceNum = tempList.length;
						this.deviceList = tempList;
						this.listenBluetooth();
					}
				});
			},
			listenBluetooth() {
				let tempList = this.deviceList;
				//监听蓝牙搜索
				uni.onBluetoothDeviceFound((res) => {
					let flag = false;
					res.devices.forEach(device => {
						if (!!device.localName) {
							tempList.push(device)
							flag = true;
						}
					})
					if (flag) {
						this.deviceList = tempList;
						this.deviceNum = this.deviceList.length;
					}
				})
			},


			getdata() {

			},
			tohome(type) {
				console.log(333);
				this.setModelType(type);
				this.setPairStatu('indexTwo');
				// 登录成功后跳转的地址
				uni.navigateTo({
					url: '/pages/home/setTime?model_type=' + type,
				});

			},
			// ArrayBuffer转16进度字符串示例
			ab2hex(buffer) {
				const hexArr = Array.prototype.map.call(
					new Uint8Array(buffer),
					function(bit) {
						return ('00' + bit.toString(16)).slice(-2)
					}
				)
				return hexArr.join('')
			},
			errMsg(code) {
				if (code === 10000) {
					uni.showToast({
						title: '未初始化蓝牙适配器',
						icon: 'none',
						duration: 2000
					})
				} else if (code === 10001) {
					uni.showToast({
						title: '当前蓝牙适配器不可用',
						icon: 'none',
						duration: 2000
					})
				} else if (code === 10002) {
					uni.showToast({
						title: '没有找到指定设备',
						icon: 'none',
						duration: 2000
					})
				} else if (code === 10003) {
					uni.showToast({
						title: '连接失败',
						icon: 'none',
						duration: 2000
					})
				} else if (code === 10004) {
					uni.showToast({
						title: '没有找到指定服务',
						icon: 'none',
						duration: 2000
					})
				} else if (code === 10005) {
					uni.showToast({
						title: '没有找到指定特征值',
						icon: 'none',
						duration: 2000
					})
				} else if (code === 10006) {
					uni.showToast({
						title: '当前连接已断开',
						icon: 'none',
						duration: 2000
					})
				} else if (code === 10007) {
					uni.showToast({
						title: '当前特征值不支持此操作',
						icon: 'none',
						duration: 2000
					})
				} else if (code === 10008) {
					uni.showToast({
						title: '其余所有系统上报的异常',
						icon: 'none',
						duration: 2000
					})
				} else if (code === 10009) {
					uni.showToast({
						title: 'Android 系统特有，系统版本低于 4.3 不支持 BLE',
						icon: 'none',
						duration: 2000
					})
				}
			},
			onUnload() {
				this.closeBluetoothAdapter();
			}
		}
	}
</script>

<style lang="scss">
	.page-content {
		width: 100vw;
		background: $uni-bg-color;
		color: $font-color-w;
		padding: $page-row-spacing;
		height: 100%;
	}

	.page-body {
		height: 100%;
		display: flex;
		flex-direction: column;

		.list-li {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: $page-row-spacing;
			background: $uni-bg-box-color;
			box-shadow: 0rpx 2rpx 9rpx 0rpx rgba(0, 0, 0, 0.44);
			border-radius: 20rpx;
			margin-bottom: 40rpx;
			color: #fff;

			.list-li-l {
				display: flex;
				font-size: 28rpx;

				.list-li-l-tit {
					margin-right: 20rpx;
				}

				.list-li-l-mon {
					height: 40rpx;
					border-radius: 20rpx;
					padding: 0 16rpx;
					font-size: 20rpx;
					font-weight: 400;
					line-height: 40rpx;
					margin-left: 16rpx;
				}
			}
		}
	}
</style>
